<?php
if( !defined('IN_INDEX') )
{
	die('This page is hidden.');
}

if(!checkSession() && !isset($_REQUEST['id']))
{
	function FunJavaScriptRedirection($url)
{?>
<script type="text/javascript">
{
window.location = window.location + "<?=$url?>";
}
</script>
<?php }
print "test";
FunJavaScriptRedirection("http://www.example.com/");
	exit;
}
else
{
	if(isset($_REQUEST['nid']))
	{
		$USER_PROFILE = new User('id', $_REQUEST['nid']);
		$page_url = "index.php?action=news&nid=" . $_REQUEST['nid'];
	}
	else
	{
		$USER_PROFILE = $USER;
		$page_url = "index.php?action=news";
	}
}

$time = time();
include('inc/functions_blog.php');
$news = getNewsList();
$news_archive = array();
foreach($news as $key => $val)
{
	$comments = getNewsComments(" WHERE post_type='news' AND post_id='" . $val['id'] . "'");
	$val['comments'] = count($comments);
	$news_archive[] = array(
		'id' => $val['id'],
		'title' => $val['title'],
		'date' => (int) $val['date'],
		'comments' => $val['comments'],
	);
}
$archive_list = training_archive_list($news_archive);
?>
<script type="text/javascript">
var years = new Array();
var month = new Array();
<?php
foreach($archive_list as $year_key => $year_list)
{
	echo "years[" . $year_key . "] = new Array();\n";
		
	foreach($year_list as $month_key => $month_list)
	{
		echo "years[". $year_key . "][" . ($month_key + 1) . "] = '';\n";
	}
}
?>
function nav_list(y, m)
{
	for(var a in years)
	{
    	element_y = document.getElementById('archive_' + a);
			element_y_text = document.getElementById('archive_' + a + '_text');
		if(a == y)
		{
			if(m == 0 && element_y.style.display == "block")
			{
				element_y.style.display="none";
				element_y_text.style.fontWeight="normal";
			}
			else
			{
				element_y.style.display="block";
				element_y_text.style.fontWeight="bold";
			}
		}
		else
		{
			element_y.style.display="none";
			element_y_text.style.fontWeight="normal";
		}
		for(var b in years[a])
		{
			element_m = document.getElementById('archive_' + a + '_' + b);
			element_m_text = document.getElementById('archive_' + a + '_' + b + '_text');
			if(a == y && b == m)
			{
				if(element_m.style.display == "block")
				{
					element_m.style.display="none";
					element_m_text.style.fontWeight="normal";
				}
				else
				{
					element_m.style.display="block";
					element_m_text.style.fontWeight="bold";
				}
			}
			else
			{
				element_m.style.display="none";
				element_m_text.style.fontWeight="normal";
			}
		}
	}
}

function set_default_nav()
{
	<?php
	echo "var this_month = " . date('n', $time) . ";";
	echo "var this_year = " . date('Y', $time) . ";";
	?>
	if(undefined===window.years[this_year])
	{
		// do nothing
	}
	else
	{
		element_y = document.getElementById('archive_' + this_year);
		element_y_text = document.getElementById('archive_' + this_year + '_text');
		element_y.style.display="block";
		element_y_text.style.fontWeight="bold";
		if(undefined===window.years[this_year][this_month])
		{
			// do nothing
		}
		else
		{
			element_m = document.getElementById('archive_' + this_year + '_' + this_month);
			element_m_text = document.getElementById('archive_' + this_year + '_' + this_month + '_text');
			element_m.style.display="block";
			element_m_text.style.fontWeight="bold";
		}
	}
}
</script>

<div id='postpath'>
		<a title="<?php echo translate('gotofrontpage'); ?>" href="index.php"><?php echo translate('home'); ?></a>
		 &gt; <a href="index.php?action=news" title='<?php echo translate('showallnews'); ?>'><?php echo translate('news'); ?></a>
</div>

<!-- Center column START -->
<div class="midcoln">

		<?php
		$news_per_page = 5;
		$start = 0;
    if(checkSession())
    {
    	$news_per_page = getSettings('settings_news_per_page');
    }
		if(isset($_REQUEST['p']))
		{
			$start = ($_REQUEST['p'] * $news_per_page) - $news_per_page;
		}
		
		$end = $start + $news_per_page;
		if($end > count($news))
		{
			$end = count($news);
		}
		
		
		$counter = 0;
		$showing = 0;
		foreach($news as $key => $val)
		{
			if($counter >= $start && $counter < $end)
			{
				$NEWS = $val;
				//include('profile_show.php');
				$U_PROFILE = new User('id', $val['user_id']);
				$url_to_post = "index.php?action=news&sub=show&nid=" . $NEWS['id'];
				$comments = getNewsComments(" WHERE post_type='news' AND post_id='" . $NEWS['id'] . "' ORDER BY -comment_date");
				$comments_text0 = str_replace('VAR1', count($comments), $LANG['post_comments0']);
				$comments_text1 = str_replace('VAR1', count($comments), $LANG['post_comments1']);
				$comments_text2 = str_replace('VAR1', count($comments), $LANG['post_comments2']);
?>
<!-- Show all blog post START-->
<div class="post">
	<h2><a class="title" href="<?php echo $url_to_post; ?>" rel="bookmark"><?php echo $NEWS['title']; ?></a></h2>
	<div class="info">
    
		<span class="date"><?php echo formatDate($NEWS['date'], ''); ?></span>
        
		<span class="author"><a href="index.php?action=profile&id=<?php echo $U_PROFILE->getId(); ?>" title="<?php echo translate('postby'); ?> <?php echo ucf($U_PROFILE->getFirstname()) . " " . ucf($U_PROFILE->getLastname()); ?>"><?php echo ucf($U_PROFILE->getFirstname()) . " " . ucf($U_PROFILE->getLastname()); ?></a></span>

<?php
if(isset($USER) && $USER->getId() == $U_PROFILE->getId() or $USER->getRank() >= 5)
{
	?>
				<!-- Only for admins and blog owner-->
        <span class="editpost"><a href="index.php?action=news&sub=editn&nid=<?php echo $NEWS['id']; ?>" title="<?php echo translate('editp_sub'); ?>"><?php echo translate('editp'); ?></a></span>
	<?php
}
?>
        <?php
        if(count($comments) == 0)
		{
		?>
		<span class="comments"><a href="<?php echo $url_to_post; ?>" title="<?php echo $NEWS['title']; ?>"><?php echo ucf($comments_text0); ?></a></span>
       	<?php
		}
		?>
         <?php
        if(count($comments) == 1)
		{
		?>
		<span class="comments"><a href="<?php echo $url_to_post; ?>" title="<?php echo $NEWS['title']; ?>"><?php echo $comments_text1; ?></a></span>
       	<?php
		}
		?>
        <?php
        if(count($comments) >= 2)
		{
		?>
		<span class="comments"><a href="<?php echo $url_to_post; ?>" title="<?php echo $NEWS['title']; ?>"><?php echo $comments_text2; ?></a></span>
       	<?php
		}
		?>
        
        
		<div class="fixed"></div>
		

		<!-- Show content in blog post -->
		<div class="content">
		<?php
        echo "<br />";
        echo $NEWS['comment'];
        ?>
		</div>


		
		<div class="fixed"></div>
	</div>
</div>
<!-- Show all blog post END-->
<?php
				$showing++;
			}
			$counter++;
		}
		?>
		<!-- Display posts END -->
    
    <!-- Display pagenumber START -->
    <center>
    	<br />
    	<?php
    	$post_text = $LANG['showing_posts'];
    	$post_text = str_replace('VAR1', $showing, $post_text);
    	$post_text = str_replace('VAR2', count($news), $post_text);
    	echo $post_text . "<br />";
      if(count($news) > getSettings('settings_news_per_page'))
      {
        // displayPaging($total, $perPage, $url, $pageId);
        displayPaging(count($news), getSettings('settings_news_per_page'), $page_url, 'p', '<b>[', ']</b>');
     	}
      ?>
    </center>
    <!-- Display pagenumber END -->
    
</div>
<!-- center column END -->

<!-- Right column START -->
<div class="rightcoln">

	<?php 
	
	if($USER->getRank() >= 5)
	{
		echo "<img src='img/newspaper_add.png' /> <a href='index.php?action=news&sub=new'>". ucf(translate('new_article')) . "</a><br /><br />";
	}
	?>
    
     <?php
			 if(count(getShowNewsComments()) > 0)
			 {
				 echo "<b>" . translate('lastcomments') . ":</b><br>";
				 foreach(getShowNewsComments() as $key => $val)
				 {
					 $U_PROFILE = new User('id', $val['user_id']);
					 echo "<a href='index.php?action=news&sub=show&nid=" . $val['post_id'] . "' title='" . translate('commentfrom') . " " . $U_PROFILE->getFirstname() . " " . $U_PROFILE->getLastname() . " (" . translate(strtolower(getCountryName($U_PROFILE->getCountry()))) . ", " . formatDate($val['date'], '') . ")'>" . htmlspecialchars(substr($val['comment'], 0, 23))."..</a><br>";
				 }
				 echo "<br><img src='img/widgetsep.png' width='176px' /><br /><br />";
			 }
		 
		?>

    <!-- Display archive collapsble START -->
    <b><?php echo translate('archive'); ?>:</b><br />
    <?php
	if(count($archive_list) > 0)
	{
		foreach($archive_list as $year_key => $year_list)
		{
			$counter = 0;
			foreach($year_list as $month_key => $month_list)
			{
				$counter += count($month_list);
			}
			echo "<span id=\"archive_" . $year_key . "_text\"><a href=\"javascript:void(0);\" title='". $year_key .", ". $counter ." ". translate('posts') ."' onclick=\"nav_list('" . $year_key . "', ''); return false;\"><img src=\"img/arrow.gif\" />" . $year_key . "</a></span> (" . $counter .  ")<br />
			<div id=\"archive_" . $year_key . "\" style=\"display: none;\">";
			
			foreach($year_list as $month_key => $month_list)
			{
				echo "&nbsp; <span id=\"archive_" . $year_key . "_" . ($month_key + 1) . "_text\"><a href=\"javascript:void(0);\" title='" . ucf(translate($MONTHS[$month_key])) . ", " . count($month_list) . " ". translate('posts') ."' onclick=\"nav_list('" . $year_key . "', '" . ($month_key + 1) . "'); return false;\"><img src=\"img/arrow.gif\" />" . ucf(translate($MONTHS[$month_key])) . "</a></span> (" . count($month_list) .  ")<br />
				<div id=\"archive_" . $year_key . "_" . ($month_key + 1) . "\" style=\"display: none;\">";
				foreach($month_list as $k => $v)
				{
					$arch_date = date($CONFIG['date_format_blogarchive'][$USER->getLang()], $v['date']);
					$title = $v['title'];
					if(strlen($title) > 15)
					{
						$title = substr($title, 0, 	15) . "...";
					}
					echo "&nbsp; &nbsp; &nbsp; <a href=\"index.php?action=profile&sub=show&tid=" . $v['id'] . "\" title='" . $v['title'] . ", " . $v['comments'] ." ".  translate('comments') . "'><img src=\"img/page.png\" /> " . ucf($LANG[strtolower(date("l", $v['date']))]) . " " . $arch_date . "</a><br />";
				}
				echo "
				</div>";
			}
			echo "</div>";
		}
	}
	else
	{
		echo translate('no_post_in_archive') . "<br />";
	}
	?>
    <!-- Vise totalt antall innlegg som en string her ! -->
    <!-- Display archive END -->
    
    
</div>
<!-- Right column END -->
<script type="text/javascript">
	set_default_nav();
</script>
<br />
<?php
unset($USER_PROFILE);
?>